package com.quyou.funtour.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quyou.funtour.bean.ResultInfo;
import com.quyou.funtour.po.ActivityImgPo;
import com.quyou.funtour.service.ActivityImgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Controller
@RequestMapping("/activityImg")
@CrossOrigin
public class ActivityImgController {

    @Autowired
    private ActivityImgService activityImgService;

    /**
     * 根据活动id获取活动图片
     * @param aid
     * @return
     */
    @RequestMapping("/getActivityImgByAid")
    @ResponseBody
    public ResultInfo getActivityImgByAid(String aid){
        List<ActivityImgPo> a_id = activityImgService.list(new QueryWrapper<ActivityImgPo>().eq("a_id", aid));
        return ResultInfo.ok(a_id);
    }

    /**
     * 根据id删除图片
     * @param url
     * @return
     */
    @RequestMapping("/deleteActivityImgBySrc")
    @ResponseBody
    public ResultInfo deleteActivityImgBySrc(String url){
        boolean b = activityImgService.remove(new QueryWrapper<ActivityImgPo>().eq("img_src",url));
        if(b){
            return ResultInfo.ok("删除成功");
        }else {
            return ResultInfo.no("删除失败");
        }
    }
    /**
     * 根据id删除图片
     * @param id
     * @return
     */
    @RequestMapping("/deleteActivityImgById/{id}")
    @ResponseBody
    public ResultInfo deleteActivityImgById(@PathVariable("id") String id){
        boolean b = activityImgService.removeById(id);
        if(b){
            return ResultInfo.ok("删除成功");
        }else {
            return ResultInfo.no("删除失败");
        }
    }

    /**
     * 添加图片
     * @param aid 活动id
     * @param url 图片地址
     * @return
     */
    @RequestMapping("/addActivityImg")
    @ResponseBody
    public ResultInfo addActivityImg(@RequestParam("aid") String aid,@RequestParam("url") String url){
        ActivityImgPo activityImgPo = new ActivityImgPo();
        activityImgPo.setAid(aid);
        activityImgPo.setUrl(url);
        boolean save = activityImgService.save(activityImgPo);
        if(save){
            return ResultInfo.ok("添加成功");
        }else {
            return ResultInfo.no("添加失败");
        }

    }

}
